import math
import numpy as np
import matplotlib.pyplot as plt
from itertools import permutations, combinations
from collections import Counter
import scipy.stats as stats

def set_chinese_font():
    """设置中文字体"""
    plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'DejaVu Sans']
    plt.rcParams['axes.unicode_minus'] = False  # 解决负号显示问题

set_chinese_font()


def analytic_geometry():
    print("=== 解析几何 ===")
    
    # 直线方程
    def line_equation(point1, point2):
        """计算两点确定的直线方程"""
        x1, y1 = point1
        x2, y2 = point2
        
        if x2 == x1:  # 垂直线
            return f"x = {x1}"
        else:
            k = (y2 - y1) / (x2 - x1)
            b = y1 - k * x1
            return f"y = {k:.2f}x + {b:.2f}"
    
    # 圆方程
    def circle_equation(center, radius):
        """圆的方程"""
        h, k = center
        return f"(x - {h})² + (y - {k})² = {radius}²"
    
    # 示例
    point_A = (1, 2)
    point_B = (3, 4)
    circle_center = (0, 0)
    circle_r = 3
    
    line_eq = line_equation(point_A, point_B)
    circle_eq = circle_equation(circle_center, circle_r)
    
    print(f"点A{point_A}和点B{point_B}确定的直线: {line_eq}")
    print(f"圆心{circle_center}半径{circle_r}的圆: {circle_eq}")
    
    # 可视化
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
    
    # 直线
    x_line = np.linspace(0, 4, 100)
    y_line = (x_line - 1) + 2  # y = x + 1
    ax1.plot(x_line, y_line, 'b-', linewidth=2, label=line_eq)
    ax1.plot([point_A[0], point_B[0]], [point_A[1], point_B[1]], 'ro', markersize=8)
    ax1.text(point_A[0], point_A[1], ' A', fontsize=12)
    ax1.text(point_B[0], point_B[1], ' B', fontsize=12)
    ax1.set_xlim(0, 4)
    ax1.set_ylim(0, 5)
    ax1.grid(True)
    ax1.set_aspect('equal')
    ax1.legend()
    ax1.set_title('直线')
    
    # 圆
    theta = np.linspace(0, 2*np.pi, 100)
    x_circle = circle_center[0] + circle_r * np.cos(theta)
    y_circle = circle_center[1] + circle_r * np.sin(theta)
    ax2.plot(x_circle, y_circle, 'r-', linewidth=2, label=circle_eq)
    ax2.plot(circle_center[0], circle_center[1], 'go', markersize=8)
    ax2.text(circle_center[0], circle_center[1], ' 圆心', fontsize=12)
    ax2.set_xlim(-4, 4)
    ax2.set_ylim(-4, 4)
    ax2.grid(True)
    ax2.set_aspect('equal')
    ax2.legend()
    ax2.set_title('圆')
    
    plt.tight_layout()
    plt.show()

analytic_geometry()